var form;
var table;
var layer;
layui.use(['layer','form','table'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;

    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#createUser").val(create.user_id);

    //加载轮播图信息
    selCouponUsed();



    //修改轮播图状态
    form.on('switch', function(data){
        var flag=data.elem.checked;
        //获取优惠券状态
        var online=null;
        if(flag){
            online=2;
        }else{
            online=3;
        }
        //优惠券id
        var coupon_id=data.elem.value;
        $.ajax({
            url: "/SyOperationController/updCouponOnline",
            dataType: "json",
            data:{"online":online,"coupon_id":coupon_id},
            success: function (data) {
                if(data.code==0){
                    layer.msg('修改成功');
                }else{
                    layer.msg('修改失败');
                }
            }
        });
    });
});
//刷新
function refresh() {
    location.reload();
}

//轮播图数据
function selCouponUsed(){
    var display=$("#display").val();
    var createUser=$("input[name='createUser']").val();
    var imgType=$("#img_type").val();
    $.ajax({
        url: "/SyOperationController/selCarouselPictures",
        type: "post",
        dataType: "json",
        data:{"display":display,"createUser":createUser,"imgType":imgType},
        success: function (data) {
            getTable(data.data);
        }
    });
}

//优惠券记录数据列表
function getTable(data){
    var tb1=table.render({
        elem:"#table_id",
        title:"优惠券记录数据列表",
        limit:5,
        data:data,
        page:true,
        cols:[[
            {type:'checkbox'},
            {field:'carousel_id',title:'用户ID',align:'center'},
            {field:'img_address',title:'图片地址',width:130,align:'center',templet:function(data) {
                    if (data.img_address != null) {
                    return "<img src='" + data.img_address + "' style='height: 100px'>";
                }else{
                    return "<span>无图片</span>"
                    }
                }},
            {field:'true_name',title:'创建人',align:'center'},
            {field:'img_title',title:'图片标题',align:'center'},
            {field:'img_type',title:'图片类型',align:'center',templet:function(data){
                    if(data.img_type==1){
                        return "商品";
                    } else if(data.img_type==2){
                        return "视频";
                    } else if(data.img_type==3){
                        return "其他";
                    }else{
                        return "无分类";
                    };
                }},
            {field:'remarks',title:'备注',align:'center',templet:function(data){
                var dat=data.remarks;
                if(dat==""){
                    return "无备注";
                }else{
                    return dat;
                }
                }},
            {field:'update_date',title:'更新时间',align:'center',templet:function(d){
                    var date=d.update_date;
                    if(date==null){
                        return "暂无更新";
                    } else{
                        date = date.slice(0, date.indexOf("."));
                        date = date.replaceAll('T',' ');
                        return date;
                    };
                }},
            {field:'',title:'操作',align:'center',templet:function(d){
                    return "<span ><a onclick='javascript:CouponDetailsSelelct(\""+d.carousel_id+"\")' style='color:#1ABC9C;'>编辑</a></span>"+ "<span style='margin-left: 20px'><a  onclick='javascript:CouponGiving(\""+d.carousel_id+"\")' style='color:#1ABC9C;'>删除</a></span>"
                }}
        ]]
    });
}
//修改轮播图
function CouponDetailsSelelct(carousel_id){
    location.href="CarouselUpdate.html?"+carousel_id;

}


function getTable2(data){
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>优惠券详情</div>",
        area : [ '670px', '450px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponDetails.html",
        success:function(layero,index){

            //处理开始时间
            var startDate=data.start_date;
            startDate = startDate.slice(0, startDate.indexOf("."));
            startDate = startDate.replaceAll('T',' ');

            //处理结束时间
            var endDate=data.end_date;
            endDate = endDate.slice(0, endDate.indexOf("."));
            endDate = endDate.replaceAll('T',' ');

            //处理轮播图状态
            var online=null;
            if(data.online==2){
                online="上线";
            }else if(data.online==3){
                online="不上线";
            }

            //获取商品状态
            var sate=null;
            if(data.product_sate==0){
                sate="删除";
            }else if(data.product_sate==1){
                sate="已上架";
            }else if(data.product_sate==2){
                sate="审核中"
            }

            //处理获取方式
            var mode=null;
            if(data.mode==2){
                mode="自动发放"
            }else if(data.mode==3){
                mode="活动领取"
            }else if(data.mode==4){
                mode="人工发放"
            }else if(data.mode==5){
                mode="无限制"
            }
            var obj=layero.find('iframe').contents();
            obj.find('input[name=couponCode]').val(data.coupon_code);
            obj.find('input[name=couponName]').val(data.coupon_name);
            obj.find('input[name=startDate]').val(startDate);
            obj.find('input[name=endDate]').val(endDate);
            obj.find('input[name=online]').val(online);
            obj.find('input[name=mode]').val(mode);
            obj.find('input[name=surplus]').val(data.surplus+" 张");
            obj.find('#remark').val(data.remark);

        }
    });
    window.layer_1=layer;
}


//删除轮播图
function CouponGiving(carousel_id) {
    $.ajax({
        url: "/SyOperationController/DeleteCarousel",
        type: "post",
        dataType: "json",
        data:{"carousel_id":carousel_id},
        success: function (data) {
            if(data.data=1){
                selCouponUsed();
            }else{
                layer.msg("删除失败")
            }
        }
    });

}